
function [YUV, polarity] = tv_gen( file_name, src_prog, src_top_first, out_prog, out_size)%out index from 0
global  g_frm_index;%start from 0
persistent    out_index;
if(isempty(out_index))
    out_index = 0;
end

if(out_prog==1)
    index = g_frm_index+1;
else
    index = floor(g_frm_index/2)+1;
end

mov = aviread(file_name, index);

if(out_prog==1)
    RGB_ = mov.cdata;
elseif((src_prog==0) && (src_top_first==0)&&(mod(out_index,2)==0)|| ...
        (src_prog==0) && (src_top_first==1)&&(mod(out_index,2)==1)|| ...
        (src_prog==1) && (mod(out_index,2)==1))
    RGB_ = mov.cdata(2:2:end, :, :);
else
    RGB_ = mov.cdata(1:2:end, :, :);
end

RGB  = cat(3, RGB_(:,:,1)' , RGB_(:,:,2)', RGB_(:,:,3)');
YUV_  = uint8(rgb2ycbcr(RGB));
real_size = size(YUV_);
if((out_size(1) == real_size(1)) && ...
    (out_size(2) == real_size(2)))
    YUV = YUV_;
else
    YUV = uint8(zeros([out_size, 3]));
    YUV(1:real_size(1), 1:real_size(2), :) = YUV_;
end
bot  = mod(g_frm_index, 2);
if(out_prog)
    polarity = 0;
else
    polarity = ~bot;
end
g_frm_index = g_frm_index+1;
out_index = out_index+1;
